import {SysNewsCategoryAdd, SysNewsCategoryData, SysNewsCategoryDetail, SysNewsCategoryParams} from "@/pages/news/category/type";
import {request} from "@@/plugin-request/request";
import download from "@/utils/download";
import {HttpResult, Page} from "@/services/typings";


export const sysNewsCategoryPageList = async (sysNewsCategoryParams: SysNewsCategoryParams) => {
  return request<HttpResult<Page<SysNewsCategoryData>>>('/api/news/category/list', {
    method: 'POST',
    data: sysNewsCategoryParams
  });
}


export const sysNewsCategoryUpdate = async (sysNewsCategoryAdd: SysNewsCategoryAdd) => {
  return request<HttpResult<string>>('/api/news/category/update', {
    method: 'POST',
    data: sysNewsCategoryAdd
  });
}

export const sysNewsCategoryAdd = async (sysNewsCategoryAdd: SysNewsCategoryAdd) => {
  return request<HttpResult<string>>('/api/news/category/add', {
    method: 'POST',
    data: sysNewsCategoryAdd
  });
}

export const sysNewsCategoryRemove = async ( ids: (number | string)[] ) => {
  return request<HttpResult<string>>('/api/news/category/remove', {
    method: 'POST',
    data: {
      ids
    }
  });
}

export const sysNewsCategoryDetail = async (id: number) => {
  return request<HttpResult<SysNewsCategoryDetail>>('/api/news/category/detail', {
    method: 'POST',
    data: {
      id
    }
  });
}

export const sysNewsCategoryImport = async (formData: FormData) => {
  return request<HttpResult<string>>('/api/news/category/import', {
    method: 'POST',
    data: formData,
    requestType: "form"
  });
}


const sysNewsCategoryExport = async (ids: (number | string)[]) => {
  return request<HttpResult<string>>('/api/news/category/export', {
    method: 'POST',
    data: {ids},
    responseType: 'blob',
    getResponse: true,
  });
}

export const downloadSysNewsCategoryExport = async (ids: (number | string)[]) => {
  await sysNewsCategoryExport(ids).then(({data, response}) => {
    download(data, response)
  });
}


const sysNewsCategoryExportTemplate = async () => {
  return request<HttpResult<string>>('/api/export/news/category/template', {
    method: 'POST',
    responseType: 'blob',
    getResponse: true,
  });
}

export const downloadSysNewsCategoryExportTemplate = async () => {
  await sysNewsCategoryExportTemplate().then(({data, response}) => {
    download(data, response)
  });
}

